package com.wzq.mvvmsmart.utils;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;
import com.wzq.mvvmsmart.R;


/**
 * Created By 刘纯贵
 * Created Time 2020/6/3
 */
public class ShowLoadingDialog {
    private static Context mContext;
    private static Dialog dialog;
    private static View inflate;
    private static LottieAnimationView lottieAnimationView;
    private static TextView textView;

    public static void showLoading(Context context) {
        showLoading(context, "加载中...");
    }

    public static void showLoading(Context context,String message){
        //mContext = context;
        dialog = new Dialog(context, R.style.MyDialog);
//        inflate = LayoutInflater.from(context).inflate(R.layout.layout_loadingview, null);
        dialog.setContentView(R.layout.layout_loadingview);
        dialog.setCanceledOnTouchOutside(false);
        LottieAnimationView lottieAnimationView = dialog.findViewById(R.id.animation_view);
        Log.i("huahua","lottieAnimationView===="+lottieAnimationView);
        lottieAnimationView.setAnimation("loading.json");
        lottieAnimationView.loop(true);
        // 设置循环
        lottieAnimationView.setRepeatCount(LottieDrawable.INFINITE);
        // 播放动画
        lottieAnimationView.playAnimation();
        textView = dialog.findViewById(R.id.text);
        textView.setText(message);
//        loadingView = inflate.findViewById(R.id.loadView);
//        loadingView.setLoadingText("加载中...");
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity( Gravity.CENTER_VERTICAL);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = dpToPx(150);  // 160dp
        lp.height = dpToPx(150); // 160dp
        lp.y = 20;
        dialogWindow.setAttributes(lp);

        if (!dialog.isShowing()) {
            dialog.show();
        }
    }

    private static int dpToPx(int dp) {
        return (int) (dp * mContext.getResources().getDisplayMetrics().density);
    }

    public void Show(){
        dialog.show();
    }

    public static void dismiss(){
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }
}
